home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / languages / cleo.lzh / Cleo / inter / source / inter.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-24  |  11.5 KB  |  348 lines

  1. /***************************************************************************
  2. *   Ce fichier, ainsi que tous les  modules  l'accompagnant, peut et  doit *
  3. * etre  copié GRATUITEMENT à la seule condition expresse de conserver      *
  4. * l'INTEGRALITE  du  Code Source, de  la documentation, et  des fichiers   *
  5. * annexes du package. Ce logiciel est Shareware, veuilez envoyer 100 FF à  *
  6. * l'auteur pour recevoir regulièrement les nouvelles versions.             *
  7. * Toute modification est INTERDITE sans l'autorisation écrite de l'auteur. *
  8. *            Tous droits réservés à M. DIALLO Barrou, Juillet 1992.        *
  9. ***************************************************************************/
  10.  
  11. /**************************************************************************/
  12. /*                 Interpreteur de Pseudo-Code Cleo                       */
  13. /*                  (C)opyright 1992 by DIALLO Barrou                     */
  14. /**************************************************************************/
  15.  
  16. #include <stdlib.h>
  17. #ifdef msdos
  18.    #include "include\\inter.h"
  19.    #include "include\\glob.h"
  20. #else
  21.    #include "include/inter.h"
  22.    #include "include/glob.h"
  23. #endif
  24.  
  25. void FreeLib(void)
  26. {
  27.     int n;
  28.     for (n=0; n<head.nbrefctlib; n++)
  29.         if( lib[n].type) cfree(lib[n].type);
  30.     cfree(lib);
  31. }
  32.  
  33. void EndInter(void)
  34. {
  35.         int n;
  36.  
  37.         if (CodeF) fclose(CodeF);
  38.         if (Char) cfree(Char);
  39.         if (Int) cfree(Int);
  40.         if (Real) cfree(Real);
  41.  
  42.         if (Vpoint3d) cfree(Vpoint3d);
  43.         if (Vpoint2d) cfree(Vpoint2d);
  44.         if (Vrgb) cfree(Vrgb);
  45.         if (Vchar) cfree(Vchar);
  46.         if (Vinteger) cfree(Vinteger);
  47.         if (Vreal) cfree(Vreal);
  48.         if (Vlongint) cfree(Vlongint);
  49.         if (Vlongreal) cfree(Vlongreal);
  50.         if (Vboolean) cfree(Vboolean);
  51.         if (stack) cfree(stack);
  52.         if (Vstring)
  53.         {
  54.          for (n=0; n< head.Vstring; n++)
  55.            if (Vstring[n]) cfree(Vstring[n]);
  56.               cfree(Vstring);
  57.         }
  58.         if (Varray)
  59.         {
  60.           for (n=0; n< head.Varray; n++)
  61.           switch(Varray[n].tab.type)
  62.           {
  63.           case integer_t:
  64.              if (Varray[n].buf.buf_int) cfree(Varray[n].buf.buf_int);
  65.              break;
  66.           case real_t:
  67.              if (Varray[n].buf.buf_real) cfree(Varray[n].buf.buf_real);
  68.              break;
  69.           case char_t:
  70.              if (Varray[n].buf.buf_char) cfree(Varray[n].buf.buf_char);
  71.              break;
  72.           case boolean_t:
  73.              if (Varray[n].buf.buf_bool) cfree(Varray[n].buf.buf_bool);
  74.              break;
  75.           case point2d_t:
  76.              if (Varray[n].buf.buf_p2d) cfree(Varray[n].buf.buf_p2d);
  77.              break;
  78.           case point3d_t:
  79.              if (Varray[n].buf.buf_p3d) cfree(Varray[n].buf.buf_p3d);
  80.              break;
  81.           case rgb_t:
  82.              if (Varray[n].buf.buf_rgb) cfree(Varray[n].buf.buf_rgb);
  83.              break;
  84.          }
  85.         cfree(Varray);
  86.         }
  87.         if (String)
  88.         {
  89.           for (n=0; n< head.string; n++)
  90.              if (String[n]) cfree(String[n]);
  91.                 cfree(String);
  92.         }
  93.         if (lib) FreeLib();
  94.  
  95.    exit(0);
  96. }
  97.  
  98. void ReadHead(char *filename)
  99. {
  100.     if (filename==(char*)NULL)            /* Nom de fichier par defaut */
  101.         {
  102.         if (!(CodeF = fopen(DEFAULT_OUT,"rb")))
  103.             {
  104.             printf("Ne peut ouvrir le fichier code par defaut\n");
  105.                                 EndInter();
  106.             }
  107.         }
  108.     else
  109.         {
  110.         if (!(CodeF = fopen(filename,"rb")))
  111.             {
  112.               printf("Ne peut ouvrir le fichier code\n");
  113.               EndInter();
  114.             }
  115.         }
  116.    fread(&head, sizeof(Entete),1, CodeF);     /* Lis l'entete */
  117.    if (strcmp(head.magic,"Cleobis"))
  118.         {
  119.               printf("Fichier Code Invalide\n");
  120.               EndInter();
  121.         }
  122.  
  123. }
  124.  
  125. void ReadCode(void)
  126. {
  127.         char *section= (char *)calloc(MAXSTRING,1);
  128.         int nbre=0, n, i;
  129.  
  130.           if (!(fread( section, strlen(SECTDATA), 1, CodeF)))   /* lis la section */
  131.             {
  132.              printf("Erreur de lecture dans le fichier Code\n");
  133.              EndInter();
  134.             }
  135.  
  136.         if ( strcmp(section, SECTDATA))
  137.            {
  138.             printf(" Section Data non presente\n");
  139.             EndInter();
  140.            }
  141.  
  142.         if (head.Char)
  143.         {
  144.           nbre= head.Char; n=0;
  145.           while (nbre--)
  146.            if (!(fread( &Char[n++], sizeof(char), 1, CodeF)))
  147.             {
  148.              printf("Erreur de lecture dans le fichier Code\n");
  149.              EndInter();
  150.             }
  151.         }
  152.         if (head.integer)
  153.         {
  154.           nbre= head.integer; n=0;
  155.           while (nbre--)
  156.             {
  157.             if (!(fread( &Int[n++], sizeof(long), 1, CodeF)))
  158.               {
  159.                 printf("Erreur de lecture dans le fichier Code\n");
  160.                 EndInter();
  161.               }
  162.             }
  163.         }
  164.         if (head.real)
  165.         {
  166.           nbre= head.real; n=0;
  167.           while (nbre--)
  168.             {
  169.             if (!(fread( &Real[n++], sizeof(double), 1, CodeF)))
  170.                  {
  171.                    printf("Erreur de lecture dans le fichier Code\n");
  172.                    EndInter();
  173.                  }
  174.             }
  175.         }
  176.         if (head.string)
  177.         {
  178.                 nbre= 0; n=0;
  179.                 while (nbre < head.string)
  180.                   {
  181.                     String[nbre] = (char *)calloc( MAXSTRING, 1);
  182.                     if (!(fread( String[nbre], MAXSTRING,1, CodeF)))
  183.                      {
  184.                        printf("Erreur de lecture dans le fichier Code\n");
  185.                        EndInter();
  186.                      }
  187.                     nbre++ ;
  188.                   }
  189.         }
  190.  
  191.         if (head.Varray)
  192.         {
  193.  
  194.            nbre= 0; n=0;
  195.            while (nbre < head.Varray)
  196.              {
  197.                if (!(fread( &Varray[nbre].tab, sizeof(int)*3,1, CodeF)))
  198.                 {
  199.                  printf("Erreur de lecture dans le fichier Code\n");
  200.                  EndInter();
  201.                 }
  202.                switch(Varray[nbre].tab.type)
  203.                {
  204.                case integer_t:
  205.                 if (!(Varray[nbre].buf.buf_int = (long *)calloc( Varray[nbre].tab.borne2-Varray[nbre].tab.borne1+1, sizeof(long))))
  206.                     {printf("Erreur d'alloc Varray\n"); EndInter(); }
  207.                 break;
  208.                case real_t:
  209.                 if (!(Varray[nbre].buf.buf_real = (double *)calloc( Varray[nbre].tab.borne2-Varray[nbre].tab.borne1+1, sizeof(double))))
  210.                     {printf("Erreur d'alloc Varray\n"); EndInter(); }
  211.                 break;
  212.                case char_t:
  213.                 if (!(Varray[nbre].buf.buf_char = (char *)calloc( Varray[nbre].tab.borne2-Varray[nbre].tab.borne1+1, sizeof(char))))
  214.                     {printf("Erreur d'alloc Varray\n"); EndInter(); }
  215.                 break;
  216.                case boolean_t:
  217.                 if (!(Varray[nbre].buf.buf_bool = (char *)calloc( Varray[nbre].tab.borne2-Varray[nbre].tab.borne1+1, sizeof(unsigned char))))
  218.                     {printf("Erreur d'alloc Varray\n"); EndInter(); }
  219.                 break;
  220.                case point2d_t:
  221.                 if (!(Varray[nbre].buf.buf_p2d = (Point2d *)calloc( Varray[nbre].tab.borne2-Varray[nbre].tab.borne1+1, sizeof(Point2d))))
  222.                     {printf("Erreur d'alloc Varray\n"); EndInter(); }
  223.                 break;
  224.                case point3d_t:
  225.                 if (!(Varray[nbre].buf.buf_p3d = (Point3d *)calloc( Varray[nbre].tab.borne2-Varray[nbre].tab.borne1+1, sizeof(Point3d))))
  226.                     {printf("Erreur d'alloc Varray\n"); EndInter(); }
  227.                 break;
  228.                case rgb_t:
  229.                 if (!(Varray[nbre].buf.buf_rgb = (Rgb *)calloc( Varray[nbre].tab.borne2-Varray[nbre].tab.borne1+1, sizeof(Rgb))))
  230.                     {printf("Erreur d'alloc Varray\n"); EndInter(); }
  231.                 break;
  232.                default: break;
  233.                }
  234.                 nbre++ ;
  235.              }
  236.         }
  237.         memset((char *)section,0,MAXSTRING);
  238.         if (!(fread( section, strlen(SECTLIB), 1, CodeF)))   /* lis la section */
  239.             {
  240.              printf("Erreur de lecture dans le fichier Code\n");
  241.              EndInter();
  242.             }
  243.         if ( strcmp(section, SECTLIB))
  244.            {
  245.             printf(" Section Library non presente\n");
  246.             EndInter();
  247.            }
  248.         if(head.nbrefctlib)
  249.         {
  250.         if (!(lib = (EXTLIB *)calloc( sizeof(EXTLIB), head.nbrefctlib)))
  251.             {
  252.               printf("Ne peut allouer la memoire pour les libraries\n");
  253.               EndInter();
  254.             }
  255.  
  256.           nbre= head.nbrefctlib; n=0;
  257.           while (nbre--)
  258.             {
  259.             if (!(fread( &lib->node, sizeof(int), 1, CodeF)))
  260.                  {
  261.                    printf("Erreur de lecture dans le fichier Code\n");
  262.                    EndInter();
  263.                  }
  264.             if (!(fread( &lib->id, sizeof(int), 1, CodeF)))
  265.                  {
  266.                    printf("Erreur de lecture dans le fichier Code\n");
  267.                    EndInter();
  268.                  }
  269.             if (!(fread( &lib->retype, sizeof(int), 1, CodeF)))
  270.                  {
  271.                    printf("Erreur de lecture dans le fichier Code\n");
  272.                    EndInter();
  273.                  }
  274.             if (!(fread( &lib->nbarg, sizeof(int), 1, CodeF)))
  275.                  {
  276.                    printf("Erreur de lecture dans le fichier Code\n");
  277.                    EndInter();
  278.                  }
  279.            lib->type = (int *)calloc(sizeof(int), lib->nbarg);
  280.             for (i=0; i<lib->nbarg; i++)
  281.                 if (!(fread( &lib->type[n], sizeof(int), 1, CodeF)))
  282.                  {
  283.                    printf("Erreur de lecture dans le fichier Code\n");
  284.                    EndInter();
  285.                  }
  286.             }
  287.         }
  288.         memset((char *)section,0,MAXSTRING);
  289.         if (!(fread( section, strlen(SECTCODE), 1, CodeF)))   /* lis la section */
  290.             {
  291.              printf("Erreur de lecture dans le fichier Code\n");
  292.              EndInter();
  293.             }
  294.         if ( strcmp(section, SECTCODE))
  295.            {
  296.             printf(" Section Code non presente\n");
  297.             EndInter();
  298.            }
  299.  
  300.         if ( !(prg = (PRG *)calloc( sizeof(PRG), head.codesize)))
  301.             {
  302.               printf("Ne peut allouer la memoire pour le code\n");
  303.               EndInter();
  304.             }
  305.  
  306.           nbre= head.codesize; n=0;
  307.           while (nbre--)
  308.             {
  309.             if (!(fread( &prg[n].code, sizeof(int), 1, CodeF)))
  310.                  {
  311.                    printf("Erreur de lecture dans le fichier Code\n");
  312.                    EndInter();
  313.                  }
  314.             if (!(fread( &prg[n].operande, sizeof(int), 1, CodeF)))
  315.                  {
  316.                    printf("Erreur de lecture dans le fichier Code\n");
  317.                    EndInter();
  318.                  }
  319.             n++;
  320.             }
  321.      cfree(section);
  322. }
  323.  
  324. void StatHead(void)
  325. {
  326. printf("**** Statistiques ***\n");
  327. printf("Taille Code:%ld \n---Constantes---\nInteger:%d \nReal:%d \nChar:%d \nString:%d\n",head.codesize, head.integer, head.real, head.Char, head.string);
  328. printf("---Variables---\nInteger:%d \nReal:%d \nChar:%d \nString:%d \nBoolean:%d \nArray:%d\n", head.Vinteger, head.Vreal, head.Vchar, head.Vstring, head.Vboolean, head.Varray);
  329. printf("---Fct externes:%d\n",head.nbrefctlib);
  330. }
  331.  
  332. void main(int argc, char **argv)
  333. {
  334. /*    if (argc ==1)
  335.         {
  336.         printf("Cleo Language\tVersion %d.%d\n\t(C)opyright July 1992 by DIALLO Barrou\n\n",VERSION, SUBVERSION);
  337.         exit(0);
  338.         }    */
  339.     ReadHead(argv[1]);
  340. /*    StatHead();       */
  341.     AlloueConst();
  342.     AlloueVars();
  343.     ReadCode();
  344.     AlloueStack();
  345. /*    Dis();     */
  346.     Inter();
  347.     EndInter();
  348. }